package com.sub.project.mall.contorller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.sub.common.utils.StringUtils;
import com.sub.project.mall.domain.PmsProduct;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sub.framework.aspectj.lang.annotation.Log;
import com.sub.framework.aspectj.lang.enums.BusinessType;
import com.sub.project.mall.service.IPmsProductService;
import com.sub.framework.web.controller.BaseController;
import com.sub.framework.web.domain.AjaxResult;
import com.sub.common.utils.poi.ExcelUtil;
import com.sub.framework.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author sub
 * @date 2020-08-19
 */
@RestController
@RequestMapping("/mall/product")
public class PmsProductController extends BaseController {
    @Autowired
    private IPmsProductService pmsProductService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('mall:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(PmsProduct pmsProduct) {
        startPage();
        QueryWrapper<PmsProduct> queryWrapper = new QueryWrapper<PmsProduct>();
        if (pmsProduct.getBrandId() != null) {
            queryWrapper.eq("brand_id", pmsProduct.getBrandId());
        }
        if (pmsProduct.getProductCategoryId() != null) {
            queryWrapper.eq("product_category_id", pmsProduct.getProductCategoryId());
        }
        if (pmsProduct.getFeightTemplateId() != null) {
            queryWrapper.eq("feight_template_id", pmsProduct.getFeightTemplateId());
        }
        if (pmsProduct.getProductAttributeCategoryId() != null) {
            queryWrapper.eq("product_attribute_category_id", pmsProduct.getProductAttributeCategoryId());
        }
        if (StringUtils.isNotBlank(pmsProduct.getName())) {
            queryWrapper.like("name", pmsProduct.getName());
        }
        if (StringUtils.isNotBlank(pmsProduct.getPic())) {
            queryWrapper.eq("pic", pmsProduct.getPic());
        }
        if (StringUtils.isNotBlank(pmsProduct.getProductSn())) {
            queryWrapper.eq("product_sn", pmsProduct.getProductSn());
        }
        if (pmsProduct.getDeleteStatus() != null) {
            queryWrapper.eq("delete_status", pmsProduct.getDeleteStatus());
        }
        if (pmsProduct.getPublishStatus() != null) {
            queryWrapper.eq("publish_status", pmsProduct.getPublishStatus());
        }
        if (pmsProduct.getNewStatus() != null) {
            queryWrapper.eq("new_status", pmsProduct.getNewStatus());
        }
        if (pmsProduct.getRecommandStatus() != null) {
            queryWrapper.eq("recommand_status", pmsProduct.getRecommandStatus());
        }
        if (pmsProduct.getVerifyStatus() != null) {
            queryWrapper.eq("verify_status", pmsProduct.getVerifyStatus());
        }
        if (pmsProduct.getSort() != null) {
            queryWrapper.eq("sort", pmsProduct.getSort());
        }
        if (pmsProduct.getSale() != null) {
            queryWrapper.eq("sale", pmsProduct.getSale());
        }
        if (pmsProduct.getPrice() != null) {
            queryWrapper.eq("price", pmsProduct.getPrice());
        }
        if (pmsProduct.getPromotionPrice() != null) {
            queryWrapper.eq("promotion_price", pmsProduct.getPromotionPrice());
        }
        if (pmsProduct.getGiftGrowth() != null) {
            queryWrapper.eq("gift_growth", pmsProduct.getGiftGrowth());
        }
        if (pmsProduct.getGiftPoint() != null) {
            queryWrapper.eq("gift_point", pmsProduct.getGiftPoint());
        }
        if (pmsProduct.getUsePointLimit() != null) {
            queryWrapper.eq("use_point_limit", pmsProduct.getUsePointLimit());
        }
        if (StringUtils.isNotBlank(pmsProduct.getSubTitle())) {
            queryWrapper.eq("sub_title", pmsProduct.getSubTitle());
        }
        if (StringUtils.isNotBlank(pmsProduct.getDescription())) {
            queryWrapper.eq("description", pmsProduct.getDescription());
        }
        if (pmsProduct.getOriginalPrice() != null) {
            queryWrapper.eq("original_price", pmsProduct.getOriginalPrice());
        }
        if (pmsProduct.getStock() != null) {
            queryWrapper.eq("stock", pmsProduct.getStock());
        }
        if (pmsProduct.getLowStock() != null) {
            queryWrapper.eq("low_stock", pmsProduct.getLowStock());
        }
        if (StringUtils.isNotBlank(pmsProduct.getUnit())) {
            queryWrapper.eq("unit", pmsProduct.getUnit());
        }
        if (pmsProduct.getWeight() != null) {
            queryWrapper.eq("weight", pmsProduct.getWeight());
        }
        if (pmsProduct.getPreviewStatus() != null) {
            queryWrapper.eq("preview_status", pmsProduct.getPreviewStatus());
        }
        if (StringUtils.isNotBlank(pmsProduct.getServiceIds())) {
            queryWrapper.eq("service_ids", pmsProduct.getServiceIds());
        }
        if (StringUtils.isNotBlank(pmsProduct.getKeywords())) {
            queryWrapper.eq("keywords", pmsProduct.getKeywords());
        }
        if (StringUtils.isNotBlank(pmsProduct.getNote())) {
            queryWrapper.eq("note", pmsProduct.getNote());
        }
        if (StringUtils.isNotBlank(pmsProduct.getAlbumPics())) {
            queryWrapper.eq("album_pics", pmsProduct.getAlbumPics());
        }
        if (StringUtils.isNotBlank(pmsProduct.getDetailTitle())) {
            queryWrapper.eq("detail_title", pmsProduct.getDetailTitle());
        }
        if (StringUtils.isNotBlank(pmsProduct.getDetailDesc())) {
            queryWrapper.eq("detail_desc", pmsProduct.getDetailDesc());
        }
        if (StringUtils.isNotBlank(pmsProduct.getDetailHtml())) {
            queryWrapper.eq("detail_html", pmsProduct.getDetailHtml());
        }
        if (StringUtils.isNotBlank(pmsProduct.getDetailMobileHtml())) {
            queryWrapper.eq("detail_mobile_html", pmsProduct.getDetailMobileHtml());
        }
        if (pmsProduct.getPromotionStartTime() != null) {
            queryWrapper.eq("promotion_start_time", pmsProduct.getPromotionStartTime());
        }
        if (pmsProduct.getPromotionEndTime() != null) {
            queryWrapper.eq("promotion_end_time", pmsProduct.getPromotionEndTime());
        }
        if (pmsProduct.getPromotionPerLimit() != null) {
            queryWrapper.eq("promotion_per_limit", pmsProduct.getPromotionPerLimit());
        }
        if (pmsProduct.getPromotionType() != null) {
            queryWrapper.eq("promotion_type", pmsProduct.getPromotionType());
        }
        if (StringUtils.isNotBlank(pmsProduct.getProductCategoryName())) {
            queryWrapper.like("product_category_name", pmsProduct.getProductCategoryName());
        }
        if (StringUtils.isNotBlank(pmsProduct.getBrandName())) {
            queryWrapper.like("brand_name", pmsProduct.getBrandName());
        }
        List<PmsProduct> list = pmsProductService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('mall:product:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PmsProduct pmsProduct) {
        QueryWrapper<PmsProduct> queryWrapper = new QueryWrapper<PmsProduct>(pmsProduct);
        List<PmsProduct> list = pmsProductService.list(queryWrapper);
        ExcelUtil<PmsProduct> util = new ExcelUtil<PmsProduct>(PmsProduct.class);
        return util.exportExcel(list, "product");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('mall:product:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(pmsProductService.getById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('mall:product:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PmsProduct pmsProduct) {
        return toAjax(pmsProductService.save(pmsProduct) ? 1 : 0);
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('mall:product:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PmsProduct pmsProduct) {
        return toAjax(pmsProductService.updateById(pmsProduct) ? 1 : 0);
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('mall:product:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(pmsProductService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
